package de.alfenory.autobill.logic;

import de.alfenory.autobill.entity.*;
import java.util.*;

/**
 * @author alexander.fenske@alfenory.de
 */
public class AutobillDetailFacade {
    
    public static AutobillDetail getAutobillDetail(Contact contact, OrderItem oi, List<Part> partList, List<TaxType> taxTypeList) {
        AutobillDetail abd = new AutobillDetail();
        abd.setDescription(oi.getDescription());
        abd.setPartsId(oi.getPartId());
        abd.setPrice(oi.getPrice());
        abd.setQuantity(oi.getQuantity());
        abd.setSourceId(oi.getId());
        Part part = PartFacade.getPart(oi.getPartId(), partList);
        if(part!=null) {
            TaxType taxType = TaxTypeFacade.getTaxType(part.getTaxId(), taxTypeList);
            if(taxType!=null) {
                abd.setTax(TaxTypeFacade.getTaxValue(taxType, oi.getPrice()*oi.getQuantity(), contact));
            }
            else {
                abd.setTax(0.0);
            }
        }
        return abd;
    }
    
    public static List<AutobillDetail> getAutobillDetailList(Contact contact, List<OrderItem> oiList, List<Part> partList, List<TaxType> taxTypeList) {
        List<AutobillDetail> abdList = new ArrayList<AutobillDetail>();
        for(OrderItem oi : oiList) {
            abdList.add(getAutobillDetail(contact, oi, partList, taxTypeList));
        }
        return abdList;
    }
    
}